package cn.hahah.module.blog.controller.admin.userdialoguegroup;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.hahah.framework.common.pojo.PageParam;
import cn.hahah.framework.common.pojo.PageResult;
import cn.hahah.framework.common.pojo.CommonResult;
import cn.hahah.framework.common.util.object.BeanUtils;
import static cn.hahah.framework.common.pojo.CommonResult.success;

import cn.hahah.framework.excel.core.util.ExcelUtils;

import cn.hahah.framework.apilog.core.annotation.ApiAccessLog;
import static cn.hahah.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.hahah.module.blog.controller.admin.userdialoguegroup.vo.*;
import cn.hahah.module.blog.dal.dataobject.userdialoguegroup.UserDialogueGroupDO;
import cn.hahah.module.blog.service.userdialoguegroup.UserDialogueGroupService;

@Tag(name = "管理后台 - 用户对话分组")
@RestController
@RequestMapping("/blog/user-dialogue-group")
@Validated
public class UserDialogueGroupController {

    @Resource
    private UserDialogueGroupService userDialogueGroupService;

    @PostMapping("/create")
    @Operation(summary = "创建用户对话分组")
    @PreAuthorize("@ss.hasPermission('blog:user-dialogue-group:create')")
    public CommonResult<Long> createUserDialogueGroup(@Valid @RequestBody UserDialogueGroupSaveReqVO createReqVO) {
        return success(userDialogueGroupService.createUserDialogueGroup(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新用户对话分组")
    @PreAuthorize("@ss.hasPermission('blog:user-dialogue-group:update')")
    public CommonResult<Boolean> updateUserDialogueGroup(@Valid @RequestBody UserDialogueGroupSaveReqVO updateReqVO) {
        userDialogueGroupService.updateUserDialogueGroup(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除用户对话分组")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('blog:user-dialogue-group:delete')")
    public CommonResult<Boolean> deleteUserDialogueGroup(@RequestParam("id") Long id) {
        userDialogueGroupService.deleteUserDialogueGroup(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得用户对话分组")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('blog:user-dialogue-group:query')")
    public CommonResult<UserDialogueGroupRespVO> getUserDialogueGroup(@RequestParam("id") Long id) {
        UserDialogueGroupDO userDialogueGroup = userDialogueGroupService.getUserDialogueGroup(id);
        return success(BeanUtils.toBean(userDialogueGroup, UserDialogueGroupRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得用户对话分组分页")
    @PreAuthorize("@ss.hasPermission('blog:user-dialogue-group:query')")
    public CommonResult<PageResult<UserDialogueGroupRespVO>> getUserDialogueGroupPage(@Valid UserDialogueGroupPageReqVO pageReqVO) {
        PageResult<UserDialogueGroupDO> pageResult = userDialogueGroupService.getUserDialogueGroupPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, UserDialogueGroupRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出用户对话分组 Excel")
    @PreAuthorize("@ss.hasPermission('blog:user-dialogue-group:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportUserDialogueGroupExcel(@Valid UserDialogueGroupPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<UserDialogueGroupDO> list = userDialogueGroupService.getUserDialogueGroupPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "用户对话分组.xls", "数据", UserDialogueGroupRespVO.class,
                        BeanUtils.toBean(list, UserDialogueGroupRespVO.class));
    }

}